package com.yxhl.zoume.core.busticket.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.adapter.BaseAdapter;
import com.yxhl.zoume.common.ui.custom.RevealLayout;
import com.yxhl.zoume.core.busticket.event.ZMArrivalStationEvent;
import com.yxhl.zoume.core.busticket.event.ZMStartStationEvent;
import com.yxhl.zoume.core.busticket.model.BusSelectCityEntity;
import com.yxhl.zoume.core.busticket.ui.activity.BusStationsListActivity;
import com.yxhl.zoume.core.busticket.ui.info.ZMScheduleType;
import com.yxhl.zoume.data.rxbus.RxBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by vhawk on 16/3/22.
 */
public class BusStartStationAdapter extends BaseAdapter<BaseAdapter.BaseViewHolder> {

    private static final String TAG = "BusStartStationAdapter";


    private static final int LETTER_VIEW_TYPE = 1;

    private Context mContext;
    private List<BusSelectCityEntity> mBusSelectCityEntityList;
    private StationType mStationType;

    public BusStartStationAdapter(Context mContext, List<BusSelectCityEntity> mBusSelectCityEntityList,
                                  StationType mStationType) {
        this.mContext = mContext;
        this.mBusSelectCityEntityList = mBusSelectCityEntityList;
        this.mStationType = mStationType;
    }


    @Override
    public int getItemCount() {
        return mBusSelectCityEntityList.size();
    }

    @Override
    public int getItemViewType(int position) {
        BusSelectCityEntity busSelectCityEntity = mBusSelectCityEntityList.get(position);
        if (busSelectCityEntity != null) {
            String displayContent = busSelectCityEntity.getDisplayContent();
            if (displayContent.matches("[A-Z]{1}")) {
                return LETTER_VIEW_TYPE;
            }
        }
        return super.getItemViewType(position);
    }

    @Override
    public BaseAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        switch (viewType) {
            case LETTER_VIEW_TYPE:
                View letterView = layoutInflater.inflate(R.layout.item_bus_station_letter, parent, false);
                return new LetterViewHolder(letterView);
            default:
                View stationView = layoutInflater.inflate(R.layout.item_single_textview, parent, false);
                return new StationViewHolder(stationView);
        }
    }

    @Override
    public void onBindViewHolder(BaseAdapter.BaseViewHolder holder, int position) {
        holder.itemView.setTag(position);
        String displayContent = mBusSelectCityEntityList.get(position).getDisplayContent();
        if (displayContent.matches("[A-Z]{1}")) {

        }

        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case LETTER_VIEW_TYPE:
                LetterViewHolder letterViewHolder = (LetterViewHolder) holder;
                letterViewHolder.setContent(displayContent);
                break;
            default:
                StationViewHolder stationViewHolder = (StationViewHolder) holder;
                stationViewHolder.setContent(displayContent);
        }
    }


    protected class StationViewHolder extends BaseAdapter.BaseViewHolder {
        @BindView(R.id.tv_item)
        TextView itemTv;
        @BindView(R.id.ll_item)
        RevealLayout itemParent;

        public StationViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void setContent(String str) {
            if (str != null) {
                itemTv.setText(str);
            }
        }

        @OnClick(R.id.ll_item)
        public void onItemClick() {
            int layoutPosition = getLayoutPosition();
            if (layoutPosition < mBusSelectCityEntityList.size() && layoutPosition >= 0) {
                BusSelectCityEntity busSelectCityEntity = mBusSelectCityEntityList.get(layoutPosition);
                if (busSelectCityEntity != null) {
                    String cityName = busSelectCityEntity.getDisplayContent();
                    String cityCode = busSelectCityEntity.getCityCode();
                    List<ZMScheduleType> scheduleTypeList = busSelectCityEntity.getScheduleTypeList();

                    if (StationType.STATION_TYPE_START == mStationType) {
                        if (!TextUtils.isEmpty(cityCode)) {
                            RxBus.getInstance().send(new ZMStartStationEvent(cityCode, cityName, scheduleTypeList));
                        }
                    } else {
                        if (!TextUtils.isEmpty(cityCode)) {
                            RxBus.getInstance().send(new ZMArrivalStationEvent(cityCode, cityName, scheduleTypeList));
                        }
                    }

                    ((BusStationsListActivity) mContext).finish();
                }
            }
        }
    }

    protected class LetterViewHolder extends BaseAdapter.BaseViewHolder {

        @BindView(R.id.tv_item_bus_station_letter)
        TextView letterTv;

        public LetterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void setContent(String str) {
            if (str != null) {
                letterTv.setText(str);
            }
        }
    }

}
